﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RealtyManagementSystem.BLL.Fees.POCO;
using RealtyManagementSystem.DAL;

namespace RealtyManagementSystem.BLL.Fees
{
    public class PropertyFeeBill
    {
        private realtydbEntities _realtyContext = null;

        private Household _householder = null;

        public Household Householder
        {
            get { return _householder; }
        }

        public List<int> Years { get; set; }

        private List<FeeStandard> _standards = null;

        public List<FeeStandard> Standards
        {
            get { return _standards; }
        }

        private List<PropertyFee> _propertyFees = null;

        public List<PropertyFee> PropertyFees
        {
            get { return _propertyFees; }
        }

        public PropertyFeeBill(List<int> years, Household householder)
        {
            Years = years;
            _householder = householder;
            _standards = new List<FeeStandard>(years.Count);
            _propertyFees = new List<PropertyFee>(years.Count);

            using (_realtyContext = new realtydbEntities())
            {
                foreach (int yr in years)
                {
                    var standard = (from feest in _realtyContext.FeeStandards
                                    where feest.Year == yr && feest.FeeCategory.CategoryName == "PropertyFee"
                                    select feest).SingleOrDefault();

                    if (standard != null)
                    {
                        var chargeItem = (from chrgItem in _realtyContext.PropertyFees
                                          where chrgItem.FeeStandardID == standard.FeeStandardID
                                          && chrgItem.HousholdId == Householder.HouseholdID
                                          select chrgItem).SingleOrDefault();
                        this._standards.Add(standard);
                        if (chargeItem != null)
                            this._propertyFees.Add(chargeItem);
                    }
                }
            }
        }

        public decimal Cost()
        {
            return (decimal)(from fs in _standards
                             select fs.FeeStandardOfYear).Sum();
        }

        public decimal TotalPaidUp()
        {
            return (decimal)(from fi in _propertyFees
                             select fi.ReceivedAmount).Sum();
        }

        public BillDetails GetDetails()
        {
            var itemList = new List<FeeItem>();
            foreach (var y in Years)
            {
                var amount = (from s in Standards
                              where s.Year == y
                              select s.FeeStandardOfYear).SingleOrDefault();
                if (amount > 0)
                {
                    var paidUp = (from pf in _propertyFees
                                  where pf.FeeStandard.Year == y
                                  select pf.ReceivedAmount).SingleOrDefault();
                    var fitem = new FeeItem()
                    {
                        Year = y,
                        Amount = (decimal)(amount),
                        ReceivedAmount = (decimal)paidUp
                    };
                    itemList.Add(fitem);
                }
            }

            return new BillDetails()
            {
                FeeItems = itemList,
                TotalAmount = this.Cost(),
                TotalPaidUp = this.TotalPaidUp()
            };
        }

        //#region Dispose
        //private bool disposedValue = false;

        //protected virtual void Dispose(bool disposing)
        //{
        //    if (!this.disposedValue)
        //    {
        //        if (disposing)
        //        {
        //            _realtyContext.Dispose();
        //        }
        //    }
        //    this.disposedValue = true;
        //}

        //public void Dispose()
        //{
        //    Dispose(true);
        //    GC.SuppressFinalize(this);
        //}
        //#endregion
    }//class
}